# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=pyside2
pkgbase=mingw-w64-${_realname}-qt5
pkgname=(${MINGW_PACKAGE_PREFIX}-${_realname}-common-qt5 ${MINGW_PACKAGE_PREFIX}-python{2,3}-${_realname}-qt5)
pkgver=5.11.0
pkgrel=1
arch=('any')
url="https://doc-snapshots.qt.io/qtforpython/"
license=("LGPL")
install=${_realname}-${CARCH}.install
source=(https://download.qt.io/official_releases/QtForPython/pyside2/PySide2-${pkgver}-src/pyside-setup-everywhere-src-${pkgver}.tar.xz
        001-pyside.patch
        002-cmake-relative-paths.patch
        003-mingw-adjust-flags.patch)
sha256sums=('fbc412c4544bca308291a08a5173a949ca530d801f00b8337902a5067e490922'
            'a5b7d7542c0a75641f1b9cdcdeb46c89367df0be8a895734ab31a5105c0dc94f'
            '8ad89154490d159b3f8d32acc04e25b65e8a501313bfd12a515608beaf778ad8'
            '8ac1fcbce6eb44c35949900e4d013da204b6e4109ed20595ff5185a26dd8e4ef')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-shiboken2-qt5=${pkgver}"
             "${MINGW_PACKAGE_PREFIX}-python3-shiboken2-qt5=${pkgver}"
             "${MINGW_PACKAGE_PREFIX}-qt5")
options=('staticlibs' 'strip')

prepare() {
  cd "${srcdir}"/pyside-setup-everywhere-src-${pkgver}
  patch -p1 -i ${srcdir}/001-pyside.patch
  patch -p1 -i ${srcdir}/002-cmake-relative-paths.patch
  patch -p1 -i ${srcdir}/003-mingw-adjust-flags.patch
}

build() {
  for pyv in python2 python3; do
    local _conf=
    if [ "${pyv}" = "python2" ]; then
      _conf="-DPYTHON_CONFIG_SUFFIX=-python2.7"
    fi
    [[ -d ${srcdir}/build-${MINGW_CHOST}-${pyv} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}-${pyv}
    mkdir -p ${srcdir}/build-${MINGW_CHOST}-${pyv} 
    pushd ${srcdir}/build-${MINGW_CHOST}-${pyv} > /dev/null

    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
    ${MINGW_PREFIX}/bin/cmake \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=Release \
      ${_conf} \
      -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/${pyv}.exe \
      ../pyside-setup-everywhere-src-${pkgver}/sources/pyside2

    MSYS2_ARG_CONV_EXCL="--include-paths=;--typesystem-paths=" \
    make

    popd > /dev/null
  done

}

fix_paths() {
  local prefixwin=$(cygpath -am ${MINGW_PREFIX})
  local prefixinst=$(cygpath -am ${pkgdir}${MINGW_PREFIX})

  local _files=$(find ${pkgdir}${MINGW_PREFIX}/lib -type f -iname "*.cmake" -o -iname "*.pc")
  for f in ${_files}; do
    plain "Patching ${f}..."
    sed -e "s|${prefixwin}|${MINGW_PREFIX}|g" \
        -e "s|${prefixinst}|${MINGW_PREFIX}|g" \
        -i ${f}
  done
}

package_pyside-common() {
  pkgdesc="Provides LGPL Qt5 bindings for Python and related tools for binding generation (Common files)(mingw-w64)"

  cd ${srcdir}/build-${MINGW_CHOST}-python3
  make DESTDIR=${pkgdir} install

  rm -rf ${pkgdir}${MINGW_PREFIX}/bin
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/python*
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/libpyside*
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig
  rm ${pkgdir}${MINGW_PREFIX}/lib/cmake/PySide2-${pkgver}/PySide2Config*python*.cmake

  fix_paths
}

package_python2-pyside() {
  pkgdesc="Provides LGPL Qt5 bindings for Python and related tools for binding generation (Python2)(mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-pyside2-common-qt5=${pkgver}"
           "${MINGW_PACKAGE_PREFIX}-python2"
           "${MINGW_PACKAGE_PREFIX}-python2-shiboken2-qt5=${pkgver}"
           "${MINGW_PACKAGE_PREFIX}-qt5")

  cd ${srcdir}/build-${MINGW_CHOST}-python2
  make DESTDIR=${pkgdir} install

  #install -Dm644 PySideConfig-python2.7.cmake ${pkgdir}${MINGW_PREFIX}/lib/cmake/PySide-${pkgver}/
  mv ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/pyside{,-py2}.pc

  rm -rf ${pkgdir}${MINGW_PREFIX}/{include,share}
  rm ${pkgdir}${MINGW_PREFIX}/lib/cmake/PySide2-${pkgver}/PySide2ConfigVersion.cmake
  rm ${pkgdir}${MINGW_PREFIX}/lib/cmake/PySide2-${pkgver}/PySide2Config.cmake

  fix_paths
}

package_python3-pyside() {
  pkgdesc="Provides LGPL Qt5 bindings for Python and related tools for binding generation (Python3)(mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-pyside2-common-qt5=${pkgver}"
           "${MINGW_PACKAGE_PREFIX}-python3"
           "${MINGW_PACKAGE_PREFIX}-python3-shiboken2-qt5=${pkgver}"
           "${MINGW_PACKAGE_PREFIX}-qt5")

  cd ${srcdir}/build-${MINGW_CHOST}-python3
  make DESTDIR=${pkgdir} install

  rm -rf ${pkgdir}${MINGW_PREFIX}/{include,share}
  rm ${pkgdir}${MINGW_PREFIX}/lib/cmake/PySide2-${pkgver}/PySide2ConfigVersion.cmake
  rm ${pkgdir}${MINGW_PREFIX}/lib/cmake/PySide2-${pkgver}/PySide2Config.cmake

  fix_paths
}

# Wrappers
package_mingw-w64-i686-pyside2-common-qt5() {
  package_pyside-common
}

package_mingw-w64-i686-python2-pyside2-qt5() {
  package_python2-pyside
}

package_mingw-w64-i686-python3-pyside2-qt5() {
  package_python3-pyside
}

package_mingw-w64-x86_64-pyside2-common-qt5() {
  package_pyside-common
}

package_mingw-w64-x86_64-python2-pyside2-qt5() {
  package_python2-pyside
}

package_mingw-w64-x86_64-python3-pyside2-qt5() {
  package_python3-pyside
}
